#include "sfsis.h"
#include "debug.h"

#define MWL_LED_SHORT 200
#define MWL_LED_LONG 1000

typedef struct
{
    HWL_GpioUsed pin;
    HWL_GpioState state;
    SF_Boolen enable;
    su16 interval;
    su16 count;
} MWL_Dev_Led_Context;

typedef struct
{
    SF_Boolen enable;
    su16 count;
    su16 adcValue;
} MWL_Dev_Key_Context;

typedef struct
{
    MWL_Dev_Led_Context leds[MWL_LED_MAX];
    MWL_Dev_Key_Context key;
} MWL_Dev_Manager;

static SF_Boolen mwl_timer_enable = SF_False;
static MWL_Dev_Manager manager;

void mwl_dev_init(void)
{
    mwl_timer_enable = SF_True;
    manager.leds[MWL_LED_GREEN].count = 0;
    manager.leds[MWL_LED_GREEN].pin = HWL_PIN_LED1;
    manager.leds[MWL_LED_GREEN].interval = MWL_DEV_LED_OFF;
    manager.leds[MWL_LED_GREEN].state = HWL_LOW;
    manager.leds[MWL_LED_GREEN].enable = SF_False;

    manager.key.count = 0;
    manager.key.adcValue = 0;
    manager.key.enable = SF_True;
    hwl_adc_start();
    gCfgFirmwarePtr->adcVal = 255;
}

void i_mwl_dev_ledUpdate(void)
{
    if (manager.leds[MWL_LED_GREEN].count > manager.leds[MWL_LED_GREEN].interval)
    {
        manager.leds[MWL_LED_GREEN].enable = SF_False;
        manager.leds[MWL_LED_GREEN].count = 0;
        manager.leds[MWL_LED_GREEN].enable = SF_True;
        hwl_gpio_set(manager.leds[MWL_LED_GREEN].pin, manager.leds[MWL_LED_GREEN].state);
        manager.leds[MWL_LED_GREEN].state = !manager.leds[MWL_LED_GREEN].state;
    }
}

void mwl_dev_setLed(MWL_Dev_LedType tp, MWL_Dev_LedMode mode)
{
    gCfgFirmwarePtr->led1State = mode;
    switch (mode)
    {
    case MWL_DEV_LED_OFF:
        manager.leds[tp].enable = SF_False;
        manager.leds[tp].state = HWL_LOW;
        hwl_gpio_set(manager.leds[tp].pin, HWL_LOW);
        break;
    case MWL_DEV_LED_ON:
        manager.leds[tp].enable = SF_False;
        manager.leds[tp].state = HWL_HIGH;
        hwl_gpio_set(manager.leds[tp].pin, HWL_HIGH);
        break;
    case MWL_DEV_LED_SHORT_BLINK:
        manager.leds[tp].enable = SF_True;
        manager.leds[tp].interval = MWL_LED_SHORT;
        manager.leds[tp].count = 0;
        break;
    case MWL_DEV_LED_LONG_BLINK:
        manager.leds[tp].enable = SF_True;
        manager.leds[tp].interval = MWL_LED_LONG;
        manager.leds[tp].count = 0;
        break;
    default:
        break;
    }
}

void i_mwl_dev_updateKeyValue(void)
{
    static su8 tmpBuf[2] = {255,255};
    //每隔10ms扫描和运算一次按键值
    if (manager.key.count >= 10)
    {
        manager.key.enable = SF_False;
        manager.key.count = 0;
        manager.key.enable = SF_True;
#if (KEY_TYPE == KEY_TYPE_ADC)
        //adc key
        if (hwl_adc_isConvertFinished() == SF_True)
        {
            manager.key.adcValue = (hwl_adc_getValue() + manager.key.adcValue) >> 1 ;
            hwl_adc_start();
            tmpBuf[1] = tmpBuf[0];
            tmpBuf[0] = (manager.key.adcValue >>4);
            if(tmpBuf[1] == tmpBuf[0])
            {
                gCfgFirmwarePtr->adcVal = tmpBuf[1];
            } 
        }
#elif (KEY_TYPE == KEY_TYPE_UART)
        //TODO UART KEY FUNCIONT
#endif
    }
}

void mwl_dev_update(void)
{
    i_mwl_dev_ledUpdate();
    i_mwl_dev_updateKeyValue();
}

void mwl_dev_timerDo(void)
{
    if (manager.leds[MWL_LED_GREEN].enable == SF_True)
    {
        manager.leds[MWL_LED_GREEN].count++;
    }
    if (manager.key.enable == SF_True)
    {
        manager.key.count++;
    }
}